var MonitorTree = function (treeId, clickCallback) {
        var self = this;
        self.url = contextRoot + "system_manage/configuration_tree/doListTree";
        self.zTree = $.fn.zTree;
        self.IDMark_A = "_a";
        self.settings = {
            view: {
                dblClickExpand: false,
                showLine: true,
                selectedMulti: false
            },
            callback: {
                onClick: function (event, treeId, treeNode, clickFlag) {
                    self.nodeClick(treeNode);
                }
            }
        };
        self.init = function (initMethod) {
            ajaxPost(self.url, {}, function (result) {
                var zTree = $.fn.zTree.init($("#" + treeId), self.settings, result);
                if (initMethod) {
                    initMethod();
                }
            }, function (result) {
                $("#" + treeId).html("加载数据失败");
            });
        };
        self.initChart = function () {
            self.settingPointAddButtons();
            ajaxPost(self.url, {}, function (result) {
                var zTree = $.fn.zTree.init($("#" + treeId), self.settings, result);
            }, function (result) {
                $("#" + treeId).html("加载数据失败");
            });
        };
        self.nodeClick = function (treeNode) {
            if (clickCallback) {
                clickCallback(treeNode);
            }
        };
        self.add = function (node) {
            var treeObj = self.zTree.getZTreeObj(treeId);
            var pNode = treeObj.getNodeByParam("id", node.pid, null);
            if (pNode) {
                treeObj.addNodes(pNode, node, true);
            }
        };
        self.update = function (data) {
            var treeObj = self.zTree.getZTreeObj(treeId);
            var node = treeObj.getNodeByParam("id", data.id, null);
            if (node) {
                node.name = data.name;
                node.code = data.code;
                node.iconSkin = data.iconSkin;
                node.nodeType = data.nodeType;
                node.description = data.description;
                node.createTime = data.createTime;
                treeObj.updateNode(node);
            }
        };
        self.settingPointAddButtons = function () {
            self.settings = {
                view: {
                    dblClickExpand: false,
                    showLine: true,
                    selectedMulti: false,
                    addDiyDom: self.showPointAddButton
                },
                callback: {
                    onClick: function (event, treeId, treeNode, clickFlag) {
                        if (clickCallback) {
                            clickCallback(treeNode);
                        }
                    }
                }
            };
        };
        self.showPointAddButton = function (treeId, treeNode) {
            if (treeNode.nodeType > 3) {
                var addStr = "<span id='diyBtn_space_" + treeNode.id + "' >&nbsp;</span><span class='button add' " +
                    "id='addBtn_" + treeNode.id + "' title='" + treeNode.name + "' onfocus='this.blur();'></span>";
                var aObj = $("#" + treeNode.tId + self.IDMark_A);
                aObj.append(addStr);
            }
        };
        self.delete = function (data) {
            if (data) {
                $.each(data, function (i, d) {
                    var treeObj = self.zTree.getZTreeObj(treeId);
                    var node = treeObj.getNodeByParam("id", d, null);
                    if (node) {
                        treeObj.removeNode(node);
                    }
                })
            }
        };
        self.getDeviceNode = function (treeNode) {
            var currentNode = treeNode;
            while (currentNode && currentNode.nodeType != 2) {
                currentNode = currentNode.getParentNode();
            }

            return currentNode;
        };
        self.selectFirstDevice = function () {
            var treeObj = self.zTree.getZTreeObj(treeId);
            var root = treeObj.getNodes();
            var children = root[0].children;
            if (children) {
                $.each(children, function (i, d) {
                    if (d.nodeType == 2) {
                        treeObj.selectNode(d, false, false);
                        if (clickCallback) {
                            clickCallback(d);
                        }
                        return;
                    }
                });
            }
        };
    }
;